package com.dq.repository.user;

import com.dq.domain.service.ServiceProject;
import com.dq.domain.subscribe.Subscribe;

import com.dq.domain.org.Org;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/11/21 16:46
 */
@Repository
public interface SubscribeRepository extends JpaRepository<Subscribe,Long> {
    Subscribe findByDeletedFalseAndSubscribeNo(String subscribeNo);
    List<Subscribe> findByWxUserAndSysDeptAndTechnicianUserAndDeletedFalseAndBeginTimeAndEndTimeAndSubscribeStatusIn(SysUser wxUser, Org sysDept, SysUser technicianUser, Long beginTime, Long endTime, List<Subscribe.SubscribeStatus> statuses);

    List<Subscribe> findBySysDeptAndTechnicianUserAndDeletedFalseAndBeginTimeLessThanEqualAndEndTimeGreaterThanEqualAndSubscribeStatusIn(Org sysDept, SysUser technicianUser, Long beginTime, Long endTime, List<Subscribe.SubscribeStatus> statuses);

    List<Subscribe> findByWxUserAndTechnicianUserAndDeletedFalseAndBeginTimeAndEndTimeAndSubscribeStatusIn(SysUser wxUser, SysUser technicianUser, Long beginTime, Long endTime, List<Subscribe.SubscribeStatus> statuses);

    List<Subscribe> findByTechnicianUserAndBeginTimeAndEndTimeAndDeletedFalseAndSubscribeStatus(SysUser technicianUser, Long beginTime, Long endTime, Subscribe.SubscribeStatus subscribeStatus);
    Page<Subscribe> findBySysDeptAndWxUserAndDeletedFalse(Org sysDept, SysUser wxUser, Pageable pageable);
    Long countBySysDeptAndWxUserAndDeletedFalse(Org sysDept, SysUser wxUser);

    Page<Subscribe> findBySysDeptAndWxUserAndSubscribeStatusAndDeletedFalse(Org sysDept, SysUser wxUser, Subscribe.SubscribeStatus subscribeStatus, Pageable pageable);
    Long countBySysDeptAndWxUserAndSubscribeStatusAndDeletedFalse(Org sysDept, SysUser wxUser, Subscribe.SubscribeStatus subscribeStatus);

    Long countBySubscribeStatusAndDeletedFalseAndWxUser(Subscribe.SubscribeStatus subscribeStatus, SysUser user);

    Page<Subscribe> findByTechnicianUserAndSubscribeStatusInAndDeletedFalse(SysUser staffUser, List<Subscribe.SubscribeStatus> statuses, Pageable pageable);
    Long countByTechnicianUserAndSubscribeStatusInAndDeletedFalse(SysUser staffUser, List<Subscribe.SubscribeStatus> statuses);

    List<Subscribe> findBySubscribeStatusIn(List<Subscribe.SubscribeStatus> statuses);

    Page<Subscribe> findByTechnicianUserAndSubscribeStatusAndDeletedFalse(SysUser staffUser, Subscribe.SubscribeStatus subscribeStatus, Pageable pageable);
    Long countByTechnicianUserAndSubscribeStatusAndDeletedFalse(SysUser staffUser, Subscribe.SubscribeStatus subscribeStatus);

    List<Subscribe> findByWxUser(WxUser wxUser);

    Page<Subscribe> findByTechnicianUserAndSubscribeStatusInAndDeletedFalseAndWxUserUserRealcName(SysUser staffUser, List<Subscribe.SubscribeStatus> statuses,String name ,Pageable pageable);

    List<Subscribe> findByTechnicianUserAndDeletedFalseAndSubscribeStatusAndBeginTimeGreaterThanAndEndTimeLessThan(SysUser staffUser, Subscribe.SubscribeStatus subscribeStatus ,Long beginTime,Long endTime);

    List<Subscribe> findByDeletedFalseAndSubscribeStatus(Subscribe.SubscribeStatus subscribeStatus);

    @Query(nativeQuery = true,value = "SELECT COALESCE(count(s.price),0) FROM `subscribe`  s  LEFT JOIN sys_user ss on s.wx_user_id = ss.id\n" +
            "where s.wx_user_id = ?1 and s.subscribe_status = ?2 and s.create_time >= ?3")
    BigDecimal countCompleted(Long wxUserId, Subscribe.SubscribeStatus subscribeStatus, Long time);

    Long countByDeletedFalseAndSubscribeStatusAndTechnicianUserAndCreateTimeGreaterThanAndCreateTimeLessThan(Subscribe.SubscribeStatus subscribeStatus, BossUser bossUser, Long beginTime , Long endTime);

    List<Subscribe> findByTechnicianUserAndDeletedFalseAndBeginTimeGreaterThanAndEndTimeLessThanAndSubscribeStatusIn(SysUser technicianUser, Long beginTime, Long endTime, List<Subscribe.SubscribeStatus> statuses);

    Long countByDeletedFalseAndSubscribeStatusAndServiceProjectAndSysDeptAndCreateTimeGreaterThanAndCreateTimeLessThan(Subscribe.SubscribeStatus subscribeStatus, ServiceProject serviceProject, Org sysSection, Long beginTime, Long endTime);
}
